import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:schedule_self_client/state_management/providers/login_status.dart';
import 'package:schedule_self_client/ui/components/setting_item.dart';
import 'package:schedule_self_client/utils/device_size_util.dart';

class UserCenterPage extends StatefulWidget {
  const UserCenterPage({super.key});

  @override
  State<UserCenterPage> createState() => _UserCenterPageState();
}

class _UserCenterPageState extends State<UserCenterPage>
    with AutomaticKeepAliveClientMixin<UserCenterPage> {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
        body: Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        children: [
          _buildUserInfoCard(),
          Expanded(
            child: GridView.count(
              crossAxisCount:
                  DeviceSizeUtil.getScreenWidth(context) > 600 ? 8 : 4,
              children: [
                SettingItem(
                  iconData: Icons.category,
                  title: '任务分类管理',
                  onTap: () {
                    GoRouter.of(context).push('/task_category_mgmt');
                  },
                )
              ],
            ),
          ),
          Center(
            child: ElevatedButton(
                onPressed: () {
                  Provider.of<LoginInfo>(context, listen: false).logout();
                },
                child: const Text('退出')),
          ),
        ],
      ),
    ));
  }

  Widget _buildUserInfoCard() {
    final height = DeviceSizeUtil.getScreenHeight(context) * 0.2;
    return SizedBox(
      width: DeviceSizeUtil.getScreenWidth(context) * 0.9,
      height: height,
      child: Row(
        children: [
          CircleAvatar(
            backgroundImage: const NetworkImage(
                'https://p3-pc-sign.douyinpic.com/tos-cn-i-dy/c32d9232aa934c8497bfb366fc289b30~noop.jpeg?biz_tag=pcweb_cover&from=327834062&s=PackSourceEnum_SEARCH&se=false&x-expires=1718938800&x-signature=a3siUjFO5o%2F74%2FOml03%2B06k%2BeAY%3D'),
            radius: height / 2 * 0.5,
          ),
          const SizedBox(width: 16.0),
          Text(
            Provider.of<LoginInfo>(context, listen: false).userInfo?.username ??
                '',
            style: Theme.of(context).textTheme.titleMedium,
          ),
        ],
      ),
    );
  }
}
